package interview.algortithm;

/**
 * @Author: zhangrongqing
 * @Date: 2020/5/19 21:14
 */
public class ReverseListNode {

    public ListNode reverse(ListNode node){
        ListNode pre = new ListNode();
        ListNode curr = node;
        while(curr.getNext() != null){
             ListNode tmp = curr.getNext();
             curr.setNext(pre);
             pre = curr;
             curr = tmp;
        }

        return pre;
    }

    public static void main(String[] args) {
        ListNode node1 = new ListNode();
        ListNode node2 = new ListNode();
        ListNode node3 = new ListNode();
        ListNode node4 = new ListNode();
        ListNode node5 = new ListNode();
        ListNode node6 = new ListNode();
        node1.setValue("1");
        node1.setNext(node2);
        node2.setValue("2");
        node2.setNext(node3);
        node3.setValue("3");
        node3.setNext(node4);
        node4.setValue("4");
        node4.setNext(node5);
        node5.setValue("5");
        node5.setNext(node6);
        node6.setValue("6");
        node6.setNext(null);

        ListNode reverse = new ReverseListNode().reverse(node1);
        System.out.println(reverse);
    }
}
